package com.live.dengdengtv.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;

import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.gson.CustomMsg;
import com.live.dengdengtv.utils.DpUtil;
import com.live.dengdengtv.view.CircleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/** An array adapter that knows how to render views when given CustomData classes */
public class CustomArrayAdapter extends ArrayAdapter<CustomMsg.CustomUserInfo> {

    private Context context;

    public CustomArrayAdapter(Context context, List<CustomMsg.CustomUserInfo> objects) {
        super(context,0, objects);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        int dp = DpUtil.dip2px(context,40);
        if (convertView == null) {
            CircleImageView circleImageView = new CircleImageView(context);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(dp, dp);
            lp.setMargins(DpUtil.dip2px(context,4),0,0,0);
            circleImageView.setLayoutParams(lp);
            circleImageView.setBorderWidth(1);
            circleImageView.setBorderColor(0xFFD2171F);
            convertView = circleImageView;

            holder = new Holder();
            holder.view = circleImageView;
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        String faceUrl = getItem(position).faceUrl;
        if(TextUtils.isEmpty(faceUrl)){
            holder.view.setImageResource(R.mipmap.head_default);
        }else {
            ImageLoader.getInstance().displayImage(faceUrl, holder.view);
        }

        return convertView;
    }

    /** View holder for the views we need access to */
    private static class Holder {
        public CircleImageView view;
    }
}
